package com.zb.utils;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.apache.commons.lang3.time.DateUtils;

import java.util.Date;
import java.util.Objects;

/**
 * 编码生成工具类
 * @Author WEN
 * @Date 2023/8/23 0023
 **/
public class CodeUtil {
    /**
     * 时间格式
     */
    private static final String DATE_YY_MM_DD = "yyMMdd";

    public static String generatorCode(String codeRule, Date date) {
        String result = "";
        if (StringUtils.isBlank(codeRule)) {
            return result;
        }
        for (String str : codeRule.split(",")) {
            if (Objects.equals("1", str)) {
                date = null == date? new Date() : date;
                result += DateFormatUtils.format(date, DATE_YY_MM_DD);
                continue;
            }
            if (Objects.equals("2", str)) {
                result += RandomUtil.random8();
                continue;
            }
            if (Objects.equals("3", str)) {
                result += RandomUtil.random8();
            }
        }
        return result;
    }


}
